/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.talbd.proventas;

import edu.upn.evolucion.proventas.entidades.Cliente;
import edu.upn.evolucion.proventas.negocio.ClienteNEG;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrador
 */
public class GestionarCliente extends javax.swing.JInternalFrame {

    /**
     * Creates new form GestionarCliente
     */
    public GestionarCliente() {
        initComponents();
        
    }

    
    private static int id;
    private static String nombre;
    private static String apellidoPaterno;
    private static String apellidoMaterno;
    private static String direccion;
    private static String dni;
    private static String ruc;
    private static String fechaRegistro;
    private static String Email;
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnBuscarCliente = new javax.swing.JButton();
        txtNombreBuscarCliente = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();
        btnRegistrar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        lblRestriccionSeleccion = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Gestionar Cliente");

        btnBuscarCliente.setText("Buscar");
        btnBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarClienteActionPerformed(evt);
            }
        });

        jLabel5.setText("Nombre:");

        tblClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nombre", "Apellido Paterno", "Apellido Materno", "Direccion", "DNI", "RUC", "Email"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblClientes);

        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        lblRestriccionSeleccion.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtNombreBuscarCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscarCliente))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnRegistrar, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnEliminar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnModificar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(lblRestriccionSeleccion))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtNombreBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscarCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnRegistrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRestriccionSeleccion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarClienteActionPerformed
        // TODO add your handling code here:
        ArrayList<Cliente> coleccion = new ArrayList<Cliente>();
        String nombreBuscar = getTxtNombreBuscarCliente().getText();

        Cliente objCliente = new Cliente();
        objCliente.setNombre(nombreBuscar);
        coleccion =ClienteNEG.obtenerInstancia().buscar(objCliente);
        DefaultTableModel temp = (DefaultTableModel) getTblClientes().getModel();
        temp.setRowCount(0);
        for(Cliente c : coleccion){
            //System.out.println(“Nombre del Empleado: ” + tmpObjeto.getNombre());
            Object nuevo[] ={c.getId(), c.getNombre(), c.getApellidoPaterno(), c.getApellidoMaterno(),c.getDireccion(),c.getDni(),c.getRuc(),c.getEmail()};
            temp.addRow(nuevo);
        }
    }//GEN-LAST:event_btnBuscarClienteActionPerformed

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        // TODO add your handling code here:
        RegistrarCliente ventana = new RegistrarCliente(null, closable);
        ventana.setVisible(true);
    }//GEN-LAST:event_btnRegistrarActionPerformed
    
    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        int fila = getTblClientes().getSelectedRow();
        if(fila == -1){
            lblRestriccionSeleccion.setText("*Seleccione");
        }
        else{
        lblRestriccionSeleccion.setText("");
        int idCliente = Integer.parseInt(getTblClientes().getValueAt(fila, 0).toString());
        String msg =ClienteNEG.obtenerInstancia().eliminar(idCliente);
        JOptionPane.showMessageDialog(null, msg);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed
   
    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
        
        TableModel tableModel = getTblClientes().getModel();
        int fila = getTblClientes().getSelectedRow();
        if(fila == -1){
            lblRestriccionSeleccion.setText("*Seleccione");
        }
        else{
         lblRestriccionSeleccion.setText("");
        setId(Integer.parseInt(tableModel.getValueAt(fila, 0).toString()));
        setNombre(tableModel.getValueAt(fila, 1).toString());
        setApellidoPaterno(tableModel.getValueAt(fila, 2).toString());
        if(tableModel.getValueAt(fila, 3) == null){
            setApellidoMaterno("");
        }
        else{
            setApellidoMaterno(tableModel.getValueAt(fila, 3).toString());
        }
        if(tableModel.getValueAt(fila, 4) == null){
            setDireccion("");
        }
        else{
            setDireccion(tableModel.getValueAt(fila, 4).toString());
        }
        if(tableModel.getValueAt(fila, 5) == null){
            setDni("");
        }
        else{
            setDni(tableModel.getValueAt(fila, 5).toString());
        }
        if(tableModel.getValueAt(fila, 6) == null){
            setRuc("");
        }
        else{
            setRuc(tableModel.getValueAt(fila, 6).toString());
        }
        if(tableModel.getValueAt(fila, 7) == null){
            setEmail("");
        }
        else{
            setEmail(tableModel.getValueAt(fila, 7).toString());
        }
        
        ModificarCliente ventana = new ModificarCliente(null, closable);
        ventana.setVisible(true);
        }
    }//GEN-LAST:event_btnModificarActionPerformed

    /**
     * @return the id
     */
    public static int getId() {
        return id;
    }

    /**
     * @param aId the id to set
     */
    public static void setId(int aId) {
        id = aId;
    }

    /**
     * @return the nombre
     */
    public static String getNombre() {
        return nombre;
    }

    /**
     * @param aNombre the nombre to set
     */
    public static void setNombre(String aNombre) {
        nombre = aNombre;
    }

    /**
     * @return the apellidoPaterno
     */
    public static String getApellidoPaterno() {
        return apellidoPaterno;
    }

    /**
     * @param aApellidoPaterno the apellidoPaterno to set
     */
    public static void setApellidoPaterno(String aApellidoPaterno) {
        apellidoPaterno = aApellidoPaterno;
    }

    /**
     * @return the apellidoMaterno
     */
    public static String getApellidoMaterno() {
        return apellidoMaterno;
    }

    /**
     * @param aApellidoMaterno the apellidoMaterno to set
     */
    public static void setApellidoMaterno(String aApellidoMaterno) {
        apellidoMaterno = aApellidoMaterno;
    }

    /**
     * @return the direccion
     */
    public static String getDireccion() {
        return direccion;
    }

    /**
     * @param aDireccion the direccion to set
     */
    public static void setDireccion(String aDireccion) {
        direccion = aDireccion;
    }

    /**
     * @return the dni
     */
    public static String getDni() {
        return dni;
    }

    /**
     * @param aDni the dni to set
     */
    public static void setDni(String aDni) {
        dni = aDni;
    }

    /**
     * @return the ruc
     */
    public static String getRuc() {
        return ruc;
    }

    /**
     * @param aRuc the ruc to set
     */
    public static void setRuc(String aRuc) {
        ruc = aRuc;
    }

    /**
     * @return the fechaRegistro
     */
    public static String getFechaRegistro() {
        return fechaRegistro;
    }

    /**
     * @param aFechaRegistro the fechaRegistro to set
     */
    public static void setFechaRegistro(String aFechaRegistro) {
        fechaRegistro = aFechaRegistro;
    }

    /**
     * @return the Email
     */
    public static String getEmail() {
        return Email;
    }

    /**
     * @param aEmail the Email to set
     */
    public static void setEmail(String aEmail) {
        Email = aEmail;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarCliente;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblRestriccionSeleccion;
    private javax.swing.JTable tblClientes;
    private javax.swing.JTextField txtNombreBuscarCliente;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the btnBuscarCliente
     */
    public javax.swing.JButton getBtnBuscarCliente() {
        return btnBuscarCliente;
    }

    /**
     * @param btnBuscarCliente the btnBuscarCliente to set
     */
    public void setBtnBuscarCliente(javax.swing.JButton btnBuscarCliente) {
        this.btnBuscarCliente = btnBuscarCliente;
    }

    /**
     * @return the btnEliminar
     */
    public javax.swing.JButton getBtnEliminar() {
        return btnEliminar;
    }

    /**
     * @param btnEliminar the btnEliminar to set
     */
    public void setBtnEliminar(javax.swing.JButton btnEliminar) {
        this.btnEliminar = btnEliminar;
    }

    /**
     * @return the btnModificar
     */
    public javax.swing.JButton getBtnModificar() {
        return btnModificar;
    }

    /**
     * @param btnModificar the btnModificar to set
     */
    public void setBtnModificar(javax.swing.JButton btnModificar) {
        this.btnModificar = btnModificar;
    }

    /**
     * @return the btnRegistrar
     */
    public javax.swing.JButton getBtnRegistrar() {
        return btnRegistrar;
    }

    /**
     * @param btnRegistrar the btnRegistrar to set
     */
    public void setBtnRegistrar(javax.swing.JButton btnRegistrar) {
        this.btnRegistrar = btnRegistrar;
    }

    /**
     * @return the jLabel5
     */
    public javax.swing.JLabel getjLabel5() {
        return jLabel5;
    }

    /**
     * @param jLabel5 the jLabel5 to set
     */
    public void setjLabel5(javax.swing.JLabel jLabel5) {
        this.jLabel5 = jLabel5;
    }

    /**
     * @return the jScrollPane1
     */
    public javax.swing.JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    /**
     * @param jScrollPane1 the jScrollPane1 to set
     */
    public void setjScrollPane1(javax.swing.JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    /**
     * @return the tblClientes
     */
    public javax.swing.JTable getTblClientes() {
        return tblClientes;
    }

    /**
     * @param tblClientes the tblClientes to set
     */
    public void setTblClientes(javax.swing.JTable tblClientes) {
        this.tblClientes = tblClientes;
    }

    /**
     * @return the txtNombreBuscarCliente
     */
    public javax.swing.JTextField getTxtNombreBuscarCliente() {
        return txtNombreBuscarCliente;
    }

    /**
     * @param txtNombreBuscarCliente the txtNombreBuscarCliente to set
     */
    public void setTxtNombreBuscarCliente(javax.swing.JTextField txtNombreBuscarCliente) {
        this.txtNombreBuscarCliente = txtNombreBuscarCliente;
    }

   
}
